package controllers;

import java.util.*;

import models.Table;
import models.Client;
import play.*;
import play.libs.*;
import play.libs.F.*;
import play.mvc.*;
import views.html.*;

import com.fasterxml.jackson.databind.JsonNode; 
import com.fasterxml.jackson.databind.node.*;

@SuppressWarnings("unused")
public class Application extends Controller {
	
	public static Game GameController = new Game();

    public static Result index() {
    	
    	String User = null;
    	String Message = "Hello!";

        return ok(index.render(User,Message));
    }
    
    public static Result join(String User) {
    	if ( User == null || User.trim().equals("") || User.length() > 20 ) {
    		//System.out.println(User);
            return ok(index.render(null,"Please choose a valid username!"));
        }
    	
    	String Message = "";
    	boolean Ready = false;
    	boolean Host = false;
    	boolean Started = false;
    	int ID;
    	
    	// dodaj nowego klienta do listy
    	ID = Clients.NewClientConnected(User);
    	
    	Host = Clients.IsHost(ID);
    	
    	if ( ID < 0 ) return ok(index.render(null,Clients.GetErrorMessage(ID))); // error / exception
    	
    	// jesli serwer jest pusty i jestesmy pierwsi
    	if ( Clients.GetClientsNumber() == 1 ) {
    		// dostajemy hosta
    		Clients.HostID = ID;
    		Host = Clients.IsHost(ID);
    		Game.GameStop();
    	}

    	if ( !Clients.IsHost(ID) ) {
    		// czekaj, az zrobi gre
    		if ( !Game.IsReady() ) {
    			System.out.println("Ready = "+Game.Ready);
    			Message = "Please wait, host is configuring a new game ...";
    			Ready = false;
    		}
    		// dolacz do jego gry
    		else
    		{
    			if ( !Game.IsRunning() ) {
    				Message = "The game is ready, join in!";
    				Ready = true;
    			}
    			else
    			{
        			Message = "The game is in progress!";
        			Ready = false;
        		}	
    		}
    	}
    	else
    	{
    		// jesli nie toczy sie zadna gra
    		if ( !Game.IsReady() ) {
    			Message = "You are the host, create a new game!";
    			Ready = false;
    		}
    		else
    		{
    			System.out.println("Ready ZLE");
    			Game.GameStop();
    			return ok();
    		}
    	}
    	
    	return ok(game.render(User,Host,Message,Ready,Started));
    }
    
    public static Result create(String User) {
    	return ok();
    	
    }
    
    public static Result quit(String User) {
    
    	int ID = Clients.GetUserID(User);
    	
    	if ( ID < 0 ) return ok(index.render(null,Clients.GetErrorMessage(ID))); // error / exception
    	
    	Clients.ClientDisconnect(ID);
    	
    	if ( Clients.GetClientsNumber() == 0 )
    		Game.GameStop();
    	
    	return ok(index.render(null,"Logged out!"));
    }
    /*
    public static Result summary(String User) {
    	
    	String Summary = GameController.getGameTable().getSummaryString();
    	
    	return ok(summary.render(User,Summary));
    }
    */
    public static Result gameJs(String User, boolean Host, boolean Ready, boolean Started) {
        return ok(views.js.game.render(User,Host,Ready,Started));
    }
    
    public static WebSocket<JsonNode> connect(final String User) {
        return new WebSocket<JsonNode>() {
            
            // Called when the Websocket Handshake is done.
            public void onReady(WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out){

            	System.out.println("WebSocket Connect - "+User);
            	// save the in/out sockets to client
            	Client C = Clients.GetClientByID(Clients.GetUserID(User));
            	
            	if ( C == null ) return;
            	
            	C.setIn(in);
            	C.setOut(out);
            	
            	Clients.SendMessageToAll("message", User+" connected!");
            	Clients.SendMessageTo(C,"username", User);
            	
                try {                	
                	// ...
                	
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        };
    }
    
    
}
